/* 
 * File:   VentanaPrincipal.cpp
 * Author: nahuel
 * 
 * Created on May 20, 2013, 8:24 PM
 */

#include "server_VentanaPrincipal.h"
#include "common_ServerConfFileManager.h"
#include "common_UsersDB.h"

#include <sstream>
#include <stdlib.h>
#include <gtkmm-3.0/gtkmm/label.h>

void VentanaPrincipal::InitValues() {
    ServerConfFileManager fileManager(_confFileName);
    
    std::string pathAu = fileManager.GetPathAU();
    _pathAu->set_text(pathAu);   
    
    std::string puertoServidor = fileManager.GetPuertoServidor();    
    _puertoServidor->set_text(puertoServidor);
    
    unsigned int timeout = fileManager.GetTimeoutClientes();
    
    std::stringstream ss;
    ss << timeout;
    std::string timeoutStr = ss.str();
    
    _timeoutClientes->set_text(timeoutStr);
}

VentanaPrincipal::VentanaPrincipal(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade) 
                    : Gtk::Window(cobject), _builder(refGlade) {
    _confFileName = "../Servidor/config.xml";
    _usersDbFileName = "../Servidor/usersdb.xml";
    
    _builder->get_widget("EntryPathAU", _pathAu);
    _builder->get_widget("EntryLabelPuertoServidor", _puertoServidor);
    _builder->get_widget("EntryTimeout", _timeoutClientes);    
    _builder->get_widget("BotonSave", _botonSave);
    
    _builder->get_widget("EntryAddUsrUsername", _addUsr_Username);
    _builder->get_widget("EntryAddUsrPsw", _addUsr_Password);
    _builder->get_widget("LabelAddUsrResult", _addUsr_Result);
    _builder->get_widget("BotonAddUsr", _addUsr_boton);
    
    _builder->get_widget("EntryDeleteUsrUsername", _delUsr_Username);    
    _builder->get_widget("LabelDeleteUsrResult", _delUsr_Result);
    _builder->get_widget("BotonDeleteUsr", _delUsr_boton);
    
    _builder->get_widget("EntryCambiarPswUsername", _changePsw_Username);
    _builder->get_widget("EntryCambiarPswPasswordNueva", _changePsw_Password);
    _builder->get_widget("LabelCambiarPswResult", _changePsw_Result);
    _builder->get_widget("BotonCambiarPsw", _changePsw_boton);
    
    InitValues();
    
    _botonSave->signal_clicked().connect(sigc::mem_fun(*this, &VentanaPrincipal::onSaveClick));    
    _addUsr_boton->signal_clicked().connect(sigc::mem_fun(*this, &VentanaPrincipal::onAddUsrButtonClick));    
    _delUsr_boton->signal_clicked().connect(sigc::mem_fun(*this, &VentanaPrincipal::onDelUsrButtonClick));    
    _changePsw_boton->signal_clicked().connect(sigc::mem_fun(*this, &VentanaPrincipal::onChangePswButtonClick));    
    
    set_size_request(700, 500);
    set_resizable(false);
    show_all_children(true);   
}

void VentanaPrincipal::onSaveClick() {
    std::string pathAu = _pathAu->get_text();    
    std::string puertoServidor = _puertoServidor->get_text();
    std::string timeoutStr = _timeoutClientes->get_text();
    
    std::stringstream ss(timeoutStr);
    unsigned int timeout;
    ss >> timeout;    
    
    ServerConfFileManager fileManager(_confFileName);
    
    fileManager.SetPathAU(pathAu);
    fileManager.SetPuertoServidor(puertoServidor);
    fileManager.SetTimeoutClientes(timeout);
    
    fileManager.SaveChanges();   
}

void VentanaPrincipal::onAddUsrButtonClick() {
    UsersDB usersDb(_usersDbFileName);
    std::string username = _addUsr_Username->get_text();
    std::string password = _addUsr_Password->get_text(); 
    
    // Si el usuario ya existe, no se puede agregar
    if (usersDb.ExisteUsuario(username)) {
        _addUsr_Result->set_text("ERROR. Nombre de usuario no disponible.");
        return;
    }
    
    usersDb.AgregarUsuario(username, password);
    _addUsr_Result->set_text("OK. Usuario agregado.");    
}

void VentanaPrincipal::onDelUsrButtonClick() {
    UsersDB usersDb(_usersDbFileName);
    std::string username = _delUsr_Username->get_text();
    
    if (!usersDb.DeleteUsuario(username)) {
        _delUsr_Result->set_text("ERROR. Usuario inexistente.");        
        return;
    }
    
    _delUsr_Result->set_text("OK. Usuario eliminado.");
}

void VentanaPrincipal::onChangePswButtonClick() {
    UsersDB usersDb(_usersDbFileName);
    std::string username = _changePsw_Username->get_text();
    std::string password = _changePsw_Password->get_text();
    
    if (!usersDb.CambiarPassword(username, password)) {
        _changePsw_Result->set_text("ERROR. Usuario inexistente.");
        return;
    }
    
    _changePsw_Result->set_text("OK. Password modificada.");
}



VentanaPrincipal::~VentanaPrincipal() {
    // TODO
}

